package com.lz.football.module.leagueMatch.view
{
	import com.common.stdlib.ArrayUtil;
	import com.common.utils.DisplayObjectUtil;
	import com.common.utils.Reflection;
	import com.global.GlobalDef;
	import com.liangzhong.events.LeagueMatchEvent;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.club.vo.NpcClubInfoVo;
	import com.lz.football.module.base.IComponent;
	import com.lz.football.module.leagueMatch.item.TeamItem;
	
	import common.BaseView.DisplayMouseEventManager;
	import common.item.ComGoodsItem;
	
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	import lz.controls.Alert;
	import lz.controls.PageComponent;
	import lz.controls.events.ListEvent;
	import lz.controls.events.PageEvent;
	import lz.controls.managers.TipsManager;

	/**
	 *基础联赛 
	 * @author lake
	 */	
	public class BaseMatchPanel extends Sprite implements ILeagueMatchPanel
	{
		/**
		 * 皮肤
		 */		
		public var _symbolSkin			:MovieClip;
		/**
		 *列表元件 
		 */		
		private var listPanel			:MovieClip;
		/**
		 *分页 
		 */		
		private var pageComp 			:PageComponent ;
		/**
		 *描述信息 
		 */		
		private var descMc				:MovieClip;
		/**
		 *球星显示容器 
		 */		
		private var starContainer		:Sprite;
		/**
		 *挑战 
		 */		
		private var challengeBtn		:SimpleButton;
		/**
		 *npc队伍信息 
		 */		
		private var _NpcList			:Array = [];
		/**
		 *列表item 
		 */		
		private var itemList			:Array = [];
		/**
		 *选择的条目 
		 */		
		private var selectItem			:TeamItem;
		/**
		 *球星选择条目 
		 */		
		private var selectStarItem		:ComGoodsItem;
		/**
		 *球星item条目 
		 */		
		private var starItemList		:Array = [];
		/**
		 *签约按钮 
		 */		
		private var signBtn			:MovieClip;
		/**
		 *签约指数mc 
		 */		
		private var signMc				:MovieClip;
		/**
		 *进度 
		 */		
		private var txtProgress		:TextField;
		
		public function BaseMatchPanel()
		{
			super();
		}
		/**
		 *初始化 
		 * 
		 */	
		public function initUI(view:Object=null):void
		{
			_symbolSkin = Reflection.createInstance("com.football.leaguematch.view.BaseMatchPanel");
			this.addChild(_symbolSkin);
			listPanel = _symbolSkin.listPanel;
			descMc = _symbolSkin.descMc;
			signBtn = descMc.signBtn;
			signMc = descMc.signMc;
			txtProgress = _symbolSkin.txtProgress;
			signBtn.visible = false;
			signMc.visible = false;
			challengeBtn = _symbolSkin.challengeBtn;
			starContainer = new Sprite();
			descMc.addChild(starContainer);
			starContainer.x = 11;
			starContainer.y = 120;
			//分页初始化
			pageComp = new PageComponent();
			pageComp.initUI(_symbolSkin.pageComp);
			
			var country :int = AppData.getInstance().clubDatamanager.clubInfo.country;
			var clubIds :Array = AppData.getInstance().leagueMatchManager.baseMatchIDs[country-1];
			var npcClubVo :NpcClubInfoVo;
			var cid :int;
			var teamItem :TeamItem;
			var i :int;
			for(i = 0;i < 5;i++)
			{
				teamItem = new TeamItem();
				teamItem.initUI(listPanel["item_" + i]);
				teamItem.setIconType(1);
				teamItem.addEventListener(ListEvent.ITEM_CLICK,itemClickHandler)
				itemList.push(teamItem);
			}
			for(i = 0;i < 12;i++)
			{
				cid = clubIds[i];
				npcClubVo = AppData.getInstance().npcClubManager.getNpcClubById(cid);
				_NpcList.push(npcClubVo);
			}
			//添加事件
			addEvtListener();
			//分页设置
			pageComp.totalNumbers = _NpcList.length;
			pageComp.pageSize = 5;
			var matchIndex :int = AppData.getInstance().leagueMatchManager.baseMatchInfoVo.matchindex;
			pageComp.curPage = 1+(matchIndex)/5;
			//选择
			selectItem = itemList[0];
			selectItem.select();
			setDescInfo();
			//进度
			if(txtProgress)
			{
				txtProgress.text = matchIndex + "/11";
			}
			//tips
			var tinfo :String = "签约指数越高签约成功率越高，打联赛可以增加签约指数！";
			DisplayMouseEventManager.instance.addTipsDisplay(signMc,tinfo);
			tinfo = "每个俱乐部的球星只能签约一次！";
			DisplayMouseEventManager.instance.addTipsDisplay(signBtn,tinfo);
		}
		/**
		 *设置描述信息 
		 */		
		private function setDescInfo():void
		{
			if(selectItem == null || selectItem.data == null)
			{
				return ;
			}
			var npcClubVo :NpcClubInfoVo = selectItem.data;
			descMc.txtName.htmlText = selectItem.data.name + "";
			descMc.txtDesc.htmlText = selectItem.data.descplay + "";
			if(selectItem.isCan)
			{
				challengeBtn.mouseEnabled = true;
				challengeBtn.filters = null;
			}
			else
			{
				//disable
				challengeBtn.mouseEnabled = false;
				challengeBtn.filters = [DisplayObjectUtil.DISABLE_FILTER];
			}
			//球星显示
			DisplayObjectUtil.removeAllChildren(starContainer);
			destroyStarItem();
			var starId :int;
			var item :ComGoodsItem;
			var i :int = 0;
			//test
			//npcClubVo.starIds = [Math.random()*800,Math.random()*800,Math.random()*800];
			for each(starId in npcClubVo.starIds)
			{
				item = new ComGoodsItem();
				item.initUI();
				item.data = AppData.getInstance().goodsManager.getStarById(starId);
				starContainer.addChild(item);
				item.x = i * (item.width + 3);
				i++;
			}
			signBtn.visible = false;
			signMc.visible = false;
			signBtn.x = starContainer.x + starContainer.width + 12;
		}
		/**
		 *注册事件 
		 * 
		 */	
		public function addEvtListener():void
		{
			this.addEventListener(ListEvent.ITEM_CLICK,itemStarClickHandler);
			challengeBtn.addEventListener(MouseEvent.CLICK,challengeHandler);
			pageComp.addEventListener(PageEvent.PAGE_CHANGE,pageChangeHandler);
			signBtn.addEventListener(MouseEvent.CLICK,signHandler);
			AppData.getInstance().leagueMatchManager.baseMatchInfoVo.addEventListener(Event.CHANGE,dataChangeHandler);
		}
		/**
		 *数据改变 
		 * @param e
		 */		
		private function dataChangeHandler(e :Event = null):void
		{
			if(e && e.bubbles)//签约相关
			{
				if(selectStarItem)
				{
					refreshStarData();
				}
				return;
			}
			var matchResList :Array = AppData.getInstance().leagueMatchManager.baseMatchInfoVo.matchResList;
			var matchIndex :int = AppData.getInstance().leagueMatchManager.baseMatchInfoVo.matchindex;
			var winNum :int = AppData.getInstance().leagueMatchManager.baseMatchInfoVo.win;
			var teamItem :TeamItem;
			var curIndexPage :int = 1+(matchIndex)/5;//可打的队伍所在的页数
			if(e)
			{
				pageComp.curPage = curIndexPage;
			}
			for(var i :int = 0;i<5;i++)
			{
				teamItem = itemList[i];
				if(teamItem.index < matchIndex)
				{
					teamItem.setScore(matchResList[teamItem.index].hgoal,matchResList[teamItem.index].ggoal);
				}
				else
				{
					teamItem.setNoneTeam();
				}
				if(teamItem.index == matchIndex)
				{
					teamItem.setCurTeam();
					teamItem.isCan = true;
				}
				else
				{
					teamItem.isCan = false;
				}
			}
			if(pageComp.curPage == curIndexPage)
			{
				selectItem = itemList[(matchIndex%5)];
				if(selectItem)
				{
					selectItem.select();
				}
			}
			setDescInfo();
			//进度
			if(txtProgress)
			{
				txtProgress.text = matchIndex + "/11";
			}
		}
		/**
		 *页数改变 
		 * @param e
		 * 
		 */		
		private function pageChangeHandler(e :PageEvent):void
		{
			if(selectItem)
			{
				selectItem.unSelect();
				selectItem = null;
			}
			//显示数据
			setItemPageDatas(ArrayUtil.getPageSizeDatas(pageComp.curPage,pageComp.pageSize,_NpcList));
		}
		/**
		 *设置数据 
		 * @param datas
		 * 
		 */		
		public function setItemPageDatas(datas :Array):void
		{
			var item :TeamItem; 
			var i :int = 0;
			for each(var itemObj :* in datas)
			{
				if(i>=5)return;
				item = itemList[i];
				item.data = itemObj;
				item.index = _NpcList.indexOf(itemObj);
				i ++;
			}
			for(;i < 5;i++)
			{
				item = itemList[i];
				item.data = null;
			}
			selectItem = itemList[0];
			selectItem.select();
			dataChangeHandler();
		}
		/**
		 *条目点击 
		 * @param e
		 * 
		 */		
		private function itemClickHandler(e :ListEvent):void
		{
			if(selectItem == e.target)
			{
				return;
			}
			if(selectItem)
			{
				selectItem.unSelect();
			}
			selectItem = e.target as TeamItem;
			selectItem.select();
			setDescInfo();
		}
		/**
		 *物品选中 
		 * @param e
		 * 
		 */		
		private function itemStarClickHandler(e :ListEvent):void
		{
			if(selectStarItem)
			{
				selectStarItem.unSelect();
				selectStarItem = null;
			}
			selectStarItem = e.target as ComGoodsItem;
			selectStarItem.select();
			refreshStarData();
		}
		/**
		 *刷新球星数据 
		 * 
		 */		
		private function refreshStarData():void
		{
			signBtn.visible = true;
			signMc.visible = true;
			var cid :int = selectItem.data.cid;
			var relationObj :* = AppData.getInstance().leagueMatchManager.baseMatchInfoVo.getClubRelationById(cid);
			signMc.txtValue.text = "" + relationObj.relation;
			var starid :int = selectStarItem.data.pid;
			var isSign:Boolean = AppData.getInstance().leagueMatchManager.baseMatchInfoVo.isStarSign(relationObj,starid);
			if(isSign)
			{
				signBtn.mouseEnabled = false;
				signBtn.mouseChildren = false;
				//signBtn.filters = [DisplayObjectUtil.DISABLE_FILTER];
				signBtn.gotoAndStop(3);
			}
			else
			{
				signBtn.gotoAndStop(1);
				signBtn.mouseEnabled = true;
				signBtn.mouseChildren = true;
				signBtn.filters = null;
			}
		}
		/**
		 *挑战 
		 * @param e
		 */		
		private function challengeHandler(e :MouseEvent):void
		{
			if(selectItem == null)
			{
				Alert.showErrorTip("未选择挑战队伍");
			}
			var param :Object = {};
			param.cid = selectItem.data.cid;
			this.dispatchEvent(new LeagueMatchEvent(LeagueMatchEvent.BASE_MATCH_START,param,true));
			//disable
			challengeBtn.mouseEnabled = false;
			challengeBtn.filters = [DisplayObjectUtil.DISABLE_FILTER];
		}
		/**
		 *签约 
		 * @param e
		 * 
		 */		
		private function signHandler(e :MouseEvent):void
		{
			var needcoin :int = selectStarItem.data.coin;
			var info :String = "签约该球星需要" + needcoin + "欧元,签约失败将扣除" + int(needcoin * .1) + "欧元。确定签约?";
			Alert.show(info,"",Alert.YES | Alert.NO,GlobalDef.AlertContainer,signAlertHandler);
		}
		/**
		 *签约确认 
		 * @param type
		 * 
		 */		
		private function signAlertHandler(type:int):void
		{
			if(type == Alert.YES)
			{
				var param :Object = {};
				param.cid = selectItem.data.cid;
				param.starid = selectStarItem.data.pid;
				this.dispatchEvent(new LeagueMatchEvent(LeagueMatchEvent.SIGN_STAR,param,true));
			}
		}
		/**
		 *删除事件 
		 * 
		 */	
		public function removeEvtListener():void
		{
			this.removeEventListener(ListEvent.ITEM_CLICK,itemStarClickHandler);
			challengeBtn.removeEventListener(MouseEvent.CLICK,challengeHandler);
			signBtn.removeEventListener(MouseEvent.CLICK,signHandler);
			pageComp.removeEventListener(PageEvent.PAGE_CHANGE,pageChangeHandler);
			AppData.getInstance().leagueMatchManager.baseMatchInfoVo.removeEventListener(Event.CHANGE,dataChangeHandler);
		}
		/**
		 *清理球星item
		 */		
		private function destroyStarItem():void
		{
			for each(var item :ComGoodsItem in starItemList)
			{
				item.destroyView();
				item = null;
			}
		}
		/**
		 *销毁 
		 * 
		 */	
		public function destroyView():void
		{
			removeEvtListener();
			destroyStarItem();
			selectStarItem = null;
			DisplayMouseEventManager.instance.removeTipsDisplay(signMc);
			DisplayObjectUtil.removeAllChildren(starContainer);
			starContainer = null;
			pageComp = null;
			_NpcList = null;
			itemList = null;
			starItemList = null;
			DisplayObjectUtil.removeAllChildren(_symbolSkin);
			_symbolSkin = null;
			listPanel = null;
			descMc = null;
			challengeBtn = null;
			signBtn = null;
			if(selectStarItem)
			{
				selectStarItem.destroyView();
				selectStarItem = null;
			}
			signMc = null;
			txtProgress = null;
		}
		/********************** implements interface *********************************/
		/**
		 * 比赛开始
		 */		
		public function matchStart(value :Object = null):void
		{
			
		}
		/**
		 * 比赛结束
		 */		
		public function matchEnd(value :Object = null):void
		{
			
		}
		/**
		 * 错误返回
		 */		
		public function errorRes(value :Object = null):void
		{
			challengeBtn.mouseEnabled = true;
			challengeBtn.filters = null;
		}
	}
}